package laspon.vvvlike.school.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import laspon.vvvlike.school.dao.StudentDao;
import laspon.vvvlike.school.dto.BaseResponse;
import laspon.vvvlike.school.dto.ProductEnum;
import laspon.vvvlike.school.dto.resquse.StudentAccountDto;
import laspon.vvvlike.school.entity.Student;
import laspon.vvvlike.school.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author by xqcw9 on 2020/9/11 14:21
 */
@Service
public class StudentServiceImpl implements StudentService {
    private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

    private StudentDao studentDao;

    @Autowired
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse studentAccount(StudentAccountDto studentAccountDto) {
        String msg = JSON.toJSONString(studentAccountDto);
        logger.info("student表入参{}", msg);
        BaseResponse response = new BaseResponse();
        int flag = studentAccountDto.getFlag();

        switch (flag) {
            case 0:
                deleteAccount(studentAccountDto, response);
                break;
            case 1:
                saveAccount(studentAccountDto, response);
                break;
            case 2:
                updateAccount(studentAccountDto, response);
                break;
            default:
                response.fail();
                logger.info("student表入参异常，请重新尝试！");
        }

        String log = JSON.toJSONString(response);
        logger.info("StudentService服务返参{}", log);
        return response;
    }

    private BaseResponse deleteAccount(StudentAccountDto studentAccountDto, BaseResponse response) {

        logger.info("student表Flag=0，删除");

        if (0 == studentAccountDto.getId()) {
            response.setRtnCode(ProductEnum.DELETE_NOT_ID.getCode());
            response.setRtnMsg(ProductEnum.DELETE_NOT_ID.getName());
            logger.info("删除操作未输入id");
            return response;
        }

        Student student = new Student();
        student.setId(studentAccountDto.getId());
        student.setValidState(ProductEnum.VALID_STATUSN.getCode());
        studentDao.updateById(student);

        response.setRtnCode(ProductEnum.SUCCESS.getCode());
        response.setRtnMsg(ProductEnum.SUCCESS.getName());
        response.setFlag("0-删除");
        return response;
    }


    private BaseResponse saveAccount(StudentAccountDto studentAccountDto, BaseResponse response) {
        if (StringUtils.isBlank(studentAccountDto.getPhoneNumber())) {
            response.setRtnCode(ProductEnum.NO_DATA.getCode());
            response.setRtnMsg(ProductEnum.NO_DATA.getName());
        }

        logger.info("student表Flag=1保存");
        Student student = new Student();
        student.setName(studentAccountDto.getName());
        student.setSex(studentAccountDto.getSex());
        student.setPhoneNumber(studentAccountDto.getPhoneNumber());
        student.setWx(studentAccountDto.getWx());
        student.setAddress(studentAccountDto.getAddress());
        student.setCreateTime(new Date());
        student.setValidState(ProductEnum.VALID_STATUSY.getCode());
        student.setUpdateTime(student.getCreateTime());
        studentDao.insert(student);

        response.setRtnCode(ProductEnum.SUCCESS.getCode());
        response.setRtnMsg(ProductEnum.SUCCESS.getName());
        response.setFlag("1-保存");
        return response;
    }

    private BaseResponse updateAccount(StudentAccountDto studentAccountDto, BaseResponse response) {

        logger.info("student表Flag=2，更新");

        if (0 == studentAccountDto.getId()) {
            response.setRtnCode(ProductEnum.UPDATE_NOT_ID.getCode());
            response.setRtnMsg(ProductEnum.UPDATE_NOT_ID.getName());
            logger.info("更新操作未输入id");
            return response;
        }

        Student student = new Student();
        student.setId(studentAccountDto.getId());
        student.setSex(studentAccountDto.getSex());
        student.setName(studentAccountDto.getName());
        student.setPhoneNumber(studentAccountDto.getPhoneNumber());
        student.setWx(studentAccountDto.getWx());
        student.setAddress(studentAccountDto.getAddress());
        student.setValidState(ProductEnum.VALID_STATUSY.getCode());
        student.setUpdateTime(new Date());
        studentDao.updateById(student);

        response.setRtnCode(ProductEnum.SUCCESS.getCode());
        response.setRtnMsg(ProductEnum.SUCCESS.getName());
        response.setFlag("2-更新");
        return response;
    }
}
